package com.tonglin.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tonglin.eduservice.entity.EduChapter;
import com.tonglin.eduservice.entity.EduVideo;
import com.tonglin.eduservice.entity.charter.ChapterVo;
import com.tonglin.eduservice.entity.charter.VideoVo;
import com.tonglin.eduservice.mapper.EduChapterMapper;
import com.tonglin.eduservice.mapper.EduVideoMapper;
import com.tonglin.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tonglin.eduservice.service.EduVideoService;
import com.tonglin.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author tonglin
 * @since 2021-05-10
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService videoService;//注入小节service
    @Autowired
    private EduChapterMapper eduChapterMapper;
    @Autowired
    private EduVideoMapper eduVideoMapper;

    //课程大纲列表,根据课程id进行查询



    @Override
    public List<ChapterVo> getChapterVideoCourseId(String courseId) {
        System.out.println("-----"+courseId);
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        List<EduChapter> list = baseMapper.selectList(wrapper);
        List<ChapterVo> chapterVoList = new ArrayList<>();
        for (EduChapter ec:list) {
            ChapterVo chapterVo = new ChapterVo();
            chapterVo.setId(ec.getId());
            chapterVo.setTitle(ec.getTitle());
            chapterVo.setChildren(getAllVideo(ec.getId(), ec.getCourseId()));
            chapterVoList.add(chapterVo);
        }
        return chapterVoList;
    }

    //删除章节的方法
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapter章节id查询小节表，如果查询数据，不能进行删除
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = videoService.count(wrapper);
        //判断
        if (count >0){
            throw new GuliException(20001,"不能删除");
        }else{
            //不能查询数据，进行删除
            //删除章节
            int result = baseMapper.deleteById(chapterId);

            return result>0;
        }


    }

    @Override
    public void deleteChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }

    public List<VideoVo> getAllVideo(String chapterId,String courseId){
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id",chapterId);
        queryWrapper.eq("course_id",courseId);
        List<EduVideo> list = eduVideoMapper.selectList(queryWrapper);
        List<VideoVo> videoVoList = new ArrayList<>();
        for (EduVideo ev: list) {
            VideoVo videoVo = new VideoVo();
            videoVo.setId(ev.getId());
            videoVo.setTitle(ev.getTitle());
            videoVoList.add(videoVo);
        }
        return videoVoList;
    }
}
